package mscontrol;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import model.dao.SurveyContentDao;
import model.dao.SurveyDetailDao;
import model.entity.SurveyContent;
import model.entity.SurveyDetail;

/**
 * Servlet implementation class GetMSSurResult
 */
@WebServlet("/GetMSSurResult")
public class GetMSSurResult extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public GetMSSurResult() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		Enumeration<String> resultPara=request.getParameterNames();
		String condit=(String)request.getParameter("condit");

		 SurveyDetail surdet=new SurveyDetail();
		 
		 for(int i=0;i<7;i++){
			 String paraName=(String)resultPara.nextElement();
			 String paraVal=request.getParameter(paraName);
			 System.out.println(paraName + " " + paraVal);
			 if(paraVal!=""){
			     switch (i) {
				case 0:
					surdet.setDate(paraVal);
					break;
				case 1:
					surdet.setPeriod(Integer.valueOf(paraVal));
					break;
				case 2:
					surdet.setScore(Integer.valueOf(paraVal));
					break;
                case 3:
					surdet.setContent_ID(Integer.valueOf(paraVal));
					break;
                case 4:
	                surdet.setTeacher_ID(Integer.valueOf(paraVal));
	                break;
                case 5:
	                surdet.setMajor_id(Integer.valueOf(paraVal));
	                break;
                case 6:
					surdet.setCourse_id(Integer.valueOf(paraVal));
					break;
				default:
					break;
				}
			 }
		 }
		 
		 SurveyDetailDao td=new SurveyDetailDao();
		 List<SurveyDetail> list=td.getRanSelect(surdet, condit);
        
		 String str="[";
		 for (SurveyDetail detail : list) {
			str+="{\"ID\":" + detail.getId() + ",\"Date\":\"" + detail.getDate() + "\",\"Period\":" + detail.getPeriod() 
					+ ",\"Score\":" + detail.getScore() + ",\"ContentID\":" + detail.getContent_ID() + ",\"TeacherID\":" +
					detail.getTeacher_ID() + ",\"MajorID\":" + detail.getMajor_id() + ",\"CourseID\":" + detail.getCourse_id() 
					+ "},";
		 }
		 
		 str=str.substring(0, str.length()-1);
		 str+="]";
		 response.setCharacterEncoding("utf-8");
		 PrintWriter out=response.getWriter();
		 out.print(str);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
